const commonjs = require('rollup-plugin-commonjs');
const nodeResolve = require('rollup-plugin-node-resolve');
const {terser} = require('rollup-plugin-terser')
const rollup = require('rollup');

function createBuild(option, callback) {
    const inputOption = {
        input: option.inputUrl,
        external: ['electron'],
        plugins: [
            commonjs({
                include: 'node_modules/**',  // Default: undefined
                exclude: ['node_modules/foo/**', 'node_modules/bar/**'],  // Default: undefined
                ignoreGlobal: false,  // Default: false
                sourceMap: false,  // Default: true
                namedExports: {'vue': ['createApp']},  // Default: undefined
                ignore: ['conditional-runtime-dependency']
            }),
            nodeResolve({
                jsnext: true,
                main: true,
                browser: true,
            }),
            terser()
        ],
        ...option.input
    }
    const outOption = {
        file: option.outputUrl,
        format: 'cjs',
        ...option.output
    }
    rollup.rollup(inputOption).then(bundle => {
        bundle.write(outOption).then(bundle => {
            callback && callback(bundle)
        })
    })
    return rollup

}

module.exports = function createRollupBuildOption(option = {
    inputUrl: undefined,
    outputUrl: undefined,
    input: {},
    output: {}

}, callback = () => {
}) {
    if (Array.isArray(option)) {
        return option.map(item => {
            return createBuild(item, callback)
        })
    } else {
        return createBuild(option, callback)
    }
}